/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.client.gwt.ui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.gadgets.client.impl.PreferencesUtil;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 *Simple DialogBox to display user messages
 */
public class SimpleDialogBox {

	private static SimpleDialogBox messeageDialogBox;

	public static SimpleDialogBox getMesseageDialogBox() {
		if (messeageDialogBox == null) {
			messeageDialogBox = new SimpleDialogBox();
		}
		return messeageDialogBox;
	}

		
	private DialogBox dialogBox;
	private HTML detailsHTML;

	protected final PreferencesUtil prefs = PreferencesUtil.nativeInitPrefs();

	private SimpleDialogBox() {
		dialogBox = new DialogBox();
		dialogBox.setAnimationEnabled(true);
				
		detailsHTML = new HTML();
		detailsHTML.setVisible(true);

		final Button closeButton = new Button(prefs.getMsg("gadgetLabelClose"));
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.setStylePrimaryName("error-panel");
		dialogVPanel
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		

		ScrollPanel panel = new ScrollPanel(detailsHTML);
		panel.setSize("200px", "100px");

		FlowPanel buttons = new FlowPanel();

		buttons.add(closeButton);
		dialogVPanel.add(panel);
		dialogVPanel.add(buttons);
		dialogBox.setWidget(dialogVPanel);
	}

	/**
	 * Display  error
	 * @param error details
	 */
	public void showError(String details) {

		dialogBox.setText(prefs.getMsg("gadgetLabelError"));
		detailsHTML.setHTML(details);
		dialogBox.center();
	}

	/**
	 * Display message
	 * @param message details
	 */
	public void showMessage(String details) {

		dialogBox.setText(prefs.getMsg("gadgetLabelMessage"));
		detailsHTML.setHTML(details);
		dialogBox.center();
	}

}
